package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.retrofitModule.response.GetCountryResponse;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ASUS on 2017/11/6.
 */

public class E164Adapter extends BaseAdapter {
    private Context mContext;
    private GetCountryResponse getCountryResponse;

    public E164Adapter(Context context, GetCountryResponse getCountryResponse){
        mContext = context;
        this.getCountryResponse = getCountryResponse;
    }

    @Override
    public int getCount() {
        return getCountryResponse.getList().size();
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.country_item, viewGroup, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }

        holder.e164.setText(getCountryResponse.getList().get(i).getCode());
        holder.name.setText(getCountryResponse.getList().get(i).getCountry());

        if(getCountryResponse.getList().get(i).getBitmap() != null) {
            holder.flag.setImageBitmap(getCountryResponse.getList().get(i).getBitmap());
        }

        return view;
    }

    static class ViewHolder {
        @BindView(R.id.e164)
        TextView e164;
        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.flag)
        ImageView flag;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public int getLetterPosition(String header){
        for(int i=0;i<getCountryResponse.getList().size();i++){
            if(getCountryResponse.getList().get(i).getFisrtLetter().equals(header)){
                return i;
            }
        }
        return -1;
    }
}
